<?php 

class Crud_Form_Decorator_File extends Zend_Form_Decorator_File
{
    /**
     * Render a form file
     *
     * @param  string $content
     * @return string
     */
    public function render($content)
    {
    	$content = parent::render($content);
    	$element = $this->getElement();
    	$value = $element->getValue();
    	if (!$value) return $content;
    	
    	$path = Config::getInstance()->file_transfer->htdocsPath;
    	$url = $path . DIRECTORY_SEPARATOR . substr($value, 0, 2). DIRECTORY_SEPARATOR . $value;
    	
    	$router = Zend_Controller_Front::getInstance()->getRouter();
    	$delete = $router->assemble(array(
    		'action' => 'clear-value',
    		'column' => $element->getName(), 
    	));
    	
    	if (preg_match("/(jpg|gif|png|jpeg)$/i", $value)) {
    		$prefix = "<a href='$url'><img src='$url' width='200' height='150' /></a> ";
    	} else {
    		$prefix = "<a href='$url'>$url</a> ";
    	}
    	
    	if (!$element->isReallyRequired) {
    		$prefix .= "<a href='$delete' id='delete_file_{$element->getName()}' class='file_delete'>Удалить файл</a> ";
    	}
		$prefix = "<div id='file_prefix_{$element->getName()}' class='file_prefix'>$prefix</div>";
    	
    	return $prefix . $content;
    }
}
